﻿/// <summary>
/// Player input.cs
/// Gibbie Chairul
/// 3/25/2015
/// 
/// This script that's going to capture all the player inputs
/// 
/// keys
/// Toggle Inventory				-Open and close the inventory window - I
/// Toggle Character Window 		-Opend and close the character information window - C
/// Move Forward					-to move the character move side or backwards - W/S
/// Rotate Player					-to have the character move die to side - Q/E
/// strafe							-to have the character move side to side = A/D
/// jump							- Button to use to make the character jump - SPACE
/// Run								- Toggle to use to have the character run or walk - SHIFT
/// </summary>

using UnityEngine;
using System.Collections;

//[AddComponentMenu("Player/All Player Scripts")]
[RequireComponent(typeof(Movement))]
[RequireComponent(typeof(PlayerCharacter))]
public class PlayerInput : MonoBehaviour {
	private float _doubleClickTimer = 0;
	private const float _DOUBLE_CLICK_TIMER_THRESHHOLD = 0.5f;
	public float attackTimer;
	public float coolDownAttack;
	// Use this for initialization
	void Start () {

		attackTimer = 0.0f;
		//coolDownAttack = GetComponent<Movement>().AttackAnimations[0].AttackTime;
		coolDownAttack = 1;
	}

	// Update is called once per frame
	void Update () {
//		for(int count = 0; count < GetComponent<Movement>().AttackAnimations.Length; count++)
//			coolDownAttack = GetComponent<Movement>().AttackAnimations[count].AttackTime;

		//Every frame the attackTimer is greater than zero it gets reduce the time that it took to render the frame
		if (attackTimer > 0)
			attackTimer -= Time.deltaTime;
		// We don't want our attackTime to go below zero
		if (attackTimer < 0)
			attackTimer = 0;


		if (Input.GetKeyUp (KeyCode.C)) {
			Messenger.Broadcast("ToggleCharacter");
		}
		if (Input.GetKeyUp (KeyCode.I)) {
			Messenger.Broadcast("ToggleInventory");
		}
		//Moving forward and backward
		
		if (Input.GetKeyDown (KeyCode.W)) {
			// if the W key is down for the first time...
			// then change flag to true, and set timer to start counting
			// Note: Detection for first time: flag is false and timer is off
			
			// if the W key is down for the second time...
			// AND the timer is within XXX msec and flag is true
			// then change flag to false, and set timer to stop counting AND DO THE DODGE!
		}
		if (Input.GetKeyUp (KeyCode.W)) {
			// if the W key is up, then 
			//		if this is for the first keydown....
			// reset flag to false
			
			// if the W key is up, then 
			//		if this is for the second keydown....
			//		then do the dodge, and reset the flag to false and timer to zero.
			
		}
		
		//if (flag == true) {
		// start moving the character FORWARD...
		//}
		
		if (Input.GetKeyDown (KeyCode.W)) {

			SendMessage("MoveMeForward", Movement.Forward.forward, SendMessageOptions.RequireReceiver);
		}
		else if (Input.GetKeyDown (KeyCode.S)){
			
			SendMessage("MoveMeForward", Movement.Forward.back, SendMessageOptions.RequireReceiver);
			
		}
		
		
		if (Input.GetKeyUp (KeyCode.W) || Input.GetKeyUp (KeyCode.S)) {
			SendMessage("MoveMeForward",Movement.Forward.none, SendMessageOptions.RequireReceiver);
		}
		
		//Rotating player
		if (Input.GetKeyDown (KeyCode.E)) {
			SendMessage("RotateMe", Movement.Turn.right, SendMessageOptions.RequireReceiver);
		}
		else if (Input.GetKeyDown (KeyCode.Q)){
			SendMessage("RotateMe", Movement.Turn.left, SendMessageOptions.RequireReceiver);
		}
		
		if (Input.GetKeyUp(KeyCode.E) || Input.GetKeyUp(KeyCode.Q)) {
			SendMessage("RotateMe",Movement.Turn.none, SendMessageOptions.RequireReceiver);
		}
		
		//Strafe player
		if (Input.GetKeyDown (KeyCode.D)) {
			SendMessage ("StrafeMe", Movement.Turn.right, SendMessageOptions.RequireReceiver);
			
		} else if (Input.GetKeyDown (KeyCode.A)) {
			
			SendMessage ("StrafeMe", Movement.Turn.left, SendMessageOptions.RequireReceiver);
			
		}
		
		
		if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D)) {
			SendMessage("StrafeMe",Movement.Turn.none, SendMessageOptions.RequireReceiver);
		}
		//Jump
		if (Input.GetKeyDown(KeyCode.Space)) {
			SendMessage("JumpMe");
		}
		//Run
		if (Input.GetKeyUp (KeyCode.LeftControl)) {
			SendMessage("ToggleRun");
		}
		
		// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
		if(Input.GetMouseButton(1)) {
			transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
		} else {
			//SendMessage("RotateMe",AdvanceMovement.Turn.none, SendMessageOptions.RequireReceiver);
			
		}

		//Movement movement = GetComponent<Movement>();
		//Attacking
		if(Input.GetMouseButtonDown(0)){
			if(attackTimer == 0)
			{
				SendMessage("MakeMeAttack",1,SendMessageOptions.RequireReceiver);
				//SendMessage("PlayMeleeAttack");
				attackTimer = coolDownAttack;
			}
		}
		
		
		
	}
}
